package com.tsugun.mediaplayer.view.loader;

import android.graphics.Bitmap;
import android.widget.ImageView;

import com.tsugun.mediaplayer.presenter.cache.DiskCache;
import com.tsugun.mediaplayer.presenter.cache.ImageCache;
import com.tsugun.mediaplayer.presenter.utils.BitmapHelper;
import com.tsugun.mediaplayer.presenter.utils.ConverterHelper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 图片加载器
 * Created by shize on 2017/3/20.
 */

public class ImageLoader {
    public static final int BIG = 1;
    public static final int ICON = 2;

    public static int IMAGE_SIZE = BIG;

    private ImageCache mImageCache;
    // 线程池
    private ExecutorService mExecutorService;
    private float mFilletRadius; // 圆角半径
    private int mCutMode; // 裁剪模式

    public ImageLoader() {
        // 设置默认缓存实现
        mImageCache = new DiskCache();
        // 设置线程池线程数量为CPU的数量
        mExecutorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
        mFilletRadius = 0;
        mCutMode = BitmapHelper.INSTANCE.getMODE_FILLET();
    }

    /**
     * 裁剪缓存图片
     *
     * @param filletRadius 圆角半径
     * @param cutMode      One of {@link BitmapHelper#MODE_FILLET},
     *                     {@link BitmapHelper#MODE_FILLET_SQUARE},
     *                     {@link BitmapHelper#MODE_CIRCLE}
     */
    public ImageLoader(float filletRadius, int cutMode) {
        this();
        mFilletRadius = filletRadius;
        mCutMode = cutMode;
    }

    /**
     * 注入缓存实现
     *
     * @param imageCache 图片缓存实现
     */
    public void setImageCache(ImageCache imageCache) {
        mImageCache = imageCache;
    }

    /**
     * @param radius  圆角半径
     * @param cutMode {@link BitmapHelper#MODE_CIRCLE}, {@link BitmapHelper#MODE_FILLET},
     *                {@link BitmapHelper#MODE_FILLET_SQUARE}
     */
    public void setCutMode(float radius, int cutMode) {
        mFilletRadius = radius;
        mCutMode = cutMode;
    }

    /**
     * 显示图片
     *
     * @param imageUrl  图片地址
     * @param imageView 图片显示容器
     * @param size      One of {@link #BIG} {@link #ICON}
     */
    public void displayImage(String imageUrl, ImageView imageView, int size) {
        if (isCacheImage(imageUrl, imageView, size)) {
            return;
        }
        // 图片没有缓存，提交到线程池中下载图片
        submitLoadRequest(imageUrl, imageView);
    }

    /**
     * 是否缓存
     *
     * @param imageUrl  string
     * @param imageView view
     * @param size      int
     * @return boolean
     */
    private boolean isCacheImage(String imageUrl, ImageView imageView, int size) {
        IMAGE_SIZE = size;
        Bitmap bitmap = mImageCache.get(imageUrl);
        IMAGE_SIZE = BIG;
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
            return true;
        }
        return false;
    }

    /**
     * 显示音乐图片
     */
    public void displayMusicImage(String musicUrl, ImageView imageView, int size) {
        if (isCacheImage(ConverterHelper.INSTANCE.getConvertedAlbumPath(musicUrl), imageView, size)) {
            return;
        }
        submitLoadRequest(musicUrl, imageView);
    }

    /**
     * 向线程池提交缓存图片请求
     *
     * @param url       图片地址
     * @param imageView 显示视图
     */
    private void submitLoadRequest(final String url, final ImageView imageView) {
        // 设置标识
        imageView.setTag(url);
        mExecutorService.submit(new Runnable() {
            @Override
            public void run() {
                Bitmap bitmap = searchImage(url);
                if (bitmap == null) {
                    return;
                }
                bitmap = BitmapHelper.INSTANCE.scissorBitmap(bitmap, mFilletRadius, mCutMode);
                // 防止图片错位
                if (imageView.getTag().equals(url)) {
                    final Bitmap finalBitmap = bitmap;
                    imageView.post(new Runnable() {
                        @Override
                        public void run() {
                            imageView.setImageBitmap(finalBitmap);
                        }
                    });
                }
                mImageCache.put(ConverterHelper.INSTANCE.getConvertedAlbumPath(url), bitmap);
            }
        });
    }

    /**
     * 根据地址选择在本地或服务器上加载图片
     *
     * @param url 图片地址
     * @return bitmap
     */
    private Bitmap searchImage(String url) {
        Bitmap bitmap;
        if (url.substring(0, 3).equals("http")) {
            bitmap = BitmapHelper.INSTANCE.getBitmapFromNetwork(url);
        } else {
            bitmap = BitmapHelper.INSTANCE.getBitmapFromMedia(url);
        }
        return bitmap;
    }
}


